package com.wgj.android.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.view.widget.XIndicatorView;
import com.wgj.android.R;
import com.wgj.android.adapter.AdverAdapter;
import com.wgj.android.events.Adver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/31.
 */

public class AdverPictureDialog extends AppCompatDialogFragment {

    private Timer timer;
    private Adver adver;
    @BindView(R.id.viewpage)
    ViewPager viewPager;
    @BindView(R.id.hongbaoView)
    View hongbaoView;
    @BindView(R.id.hongBaoImg)
    ImageView hongBaoImg;
    @BindView(R.id.hongBaoTime)
    TextView hongBaoTime;
    @BindView(R.id.indicatorview)
    XIndicatorView indicatorView;

    private JSONArray array;

    private AdverPageAdapter adverPageAdapter;

    public static AdverPictureDialog getInstael(Adver adver) {
        AdverPictureDialog qdDialog = new AdverPictureDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("adver",adver);
        qdDialog.setArguments(bundle);
        return qdDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adver= (Adver) getArguments().getSerializable("adver");
        try {

            array=JSONArray.parseArray(adver.getImgs());
        }catch (Exception e){

        }
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.adver_pricture_dialog, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }
    private void  initView(){
        adverPageAdapter=new AdverPageAdapter();
        viewPager.setAdapter(adverPageAdapter);


    }


    @OnClick(R.id.closeBut)
    public  void  closeBut(View view){
         dismiss();
    }
    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.height = XDensityUtils.getScreenHeight();
        lp.width = XDensityUtils.getScreenWidth();
        lp.gravity= Gravity.CENTER;
        window.setAttributes(lp);
        if(adver.getIs_get()==0){
            startTime();
        }
    }


    private  void  startTime(){
        if(timer!=null){
            timer.cancel();
            timer=null;

        }
        timer=new Timer(15000,1000);
        timer.start();
        hongbaoView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(timer!=null){
            timer.cancel();
            timer=null;
        }


    }

    class Timer extends CountDownTimer {


        public Timer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            hongBaoTime.setText(millisUntilFinished / 1000+"");

        }
        @Override
        public void onFinish() {
            hongbaoView.setVisibility(View.GONE);
            OpenAdverHongBaoDialog.getInstael(adver).show(getChildFragmentManager(),"OpenAdverHongBaoDialog");
        }
    }


    class  AdverPageAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return  array==null?0:array.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            JSONObject object=array.getJSONObject(position);
            View view=LayoutInflater.from(getContext()).inflate(R.layout.adver_picture_item,null);
            ImageView imageView=view.findViewById(R.id.img);
            int size=XDensityUtils.getScreenWidth();
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size));
            Glide.with(getContext()).load(object.getString("img")).into(imageView);
            ((TextView)view.findViewById(R.id.title)).setText(object.getString("title"));
            container.addView(view);
            return view;
        }
    }
}
